# coding:utf-8
from tools import DBUtil

__author__ = 'cgk'


class DictUtil:
    def __init__(self):
        pass

    def getKey(self, dict_type, value):
        """
        获取配置表KEYVALUE里对应的值和key
        @param type
        @param value
        @return
        @throws SQLException
        return String
        """
        sql = "select item_cn FROM cm_dict where dict_type='%s' and item_value='%s'" % (dict_type, value)
        # resultList = [{}] # List<HashMap<String,String>>

        resultList = DBUtil.executeQuery(sql)
        if(len(resultList) == 0):
            resultString = ""
        else:
            resultString = resultList[0][0] # resultList.get(0).get("item_cn")


        return resultString.decode(encoding='gbk')


    def queryKeyValueList(self, dict_type):
        """
        获取KeyValue表中某一类
        @param cm_type
        @return [{}]  --  List<HashMap<String, String>>
        """
        sql = "SELECT item_cn,item_value FROM cm_dict where dict_type= '%s' order by item_order" % dict_type

        try:
            resultList = DBUtil.executeQuery(sql)
        except Exception,e:
            print "查询KeyValue表出错！"
            print e


        return resultList


    def getKeyValueList(self, cm_type, fieldname=None, init=None):
        """
        初始化下拉数据
        :param fieldname:
        :param cm_type:
        :param init:
        :return: void
        """
        list = [{}] # List<HashMap<String, String>>
        list = self.queryKeyValueList(cm_type)

        for item in list:
            item_cn = item[0] # map.get("item_cn")
            item_value = item[1] # map.get("item_value")
            # init.addComboValue(fieldname, item_cn, item_value)



    def getKeyValueListWithSql(self, sql, fieldname=None, init=None):
        """

        :param fieldname:
        :param sql:
        :param init:
        :return: void
        """
        resultList = [{}] # List<HashMap<String,String>>
        try:
            resultList = DBUtil.executeQuery(sql)
        except Exception, e:
            print e
            return

        for item in resultList:
            item_cn = item[0] # map.get("item_cn")
            item_value = item[1] # map.get("item_value")
            # init.addComboValue(fieldname, item_cn, item_value)


    def queryKeyValueMap(self, cm_type):
        """
        获取KeyValue表中某一类信息
        :param cm_type:
        :return: HashMap<String, String>
        """
        keyValueMap = {} # new HashMap<String, String>();
        resultList = [{}] # new ArrayList<HashMap<String,String>>();
        sql = "SELECT item_cn,item_value FROM cm_dict where dict_type='%s' order by item_order" % cm_type
        try:
            resultList = DBUtil.executeQuery(sql)
            if resultList != None and len(resultList)>0:
                for map in resultList:
                    # keyValueMap.put(map.get("item_value"), map.get("item_cn"))
                    keyValueMap[map[0]] = map[1]

        except Exception, e:
            print "查询KeyValue表出错！"
            print e
        return keyValueMap


if __name__ == '__main__':
    dict_type = 'risk.tradetype'
    dict = DictUtil()

    print "\ngetKey"
    print dict.getKey(dict_type,1)

    print "\nqueryKeyValueList"
    for item_cn,item_value in dict.queryKeyValueList('risk.tradetype'):
        print item_cn.decode('gbk'), item_value.decode('gbk')

    print "\ngetKeyValueList"
    dict.getKeyValueList('risk.tradetype')

    print "\ngetKeyValueListWithSql"
    sql = "SELECT item_cn,item_value FROM cm_dict where dict_type= '%s' order by item_order" % dict_type
    dict.getKeyValueListWithSql(sql)

    print "\nqueryKeyValueMap"
    for key,value in dict.queryKeyValueMap(dict_type).items():
        print key.decode('gbk'),value
    print dict.queryKeyValueMap(dict_type)